<?php

namespace App\Http\Controllers\Weixin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Auth;

use App\Repositories\Eloquent\Biz\WeixinRepository as Weixin;
use App\Repositories\Eloquent\Exm\WarehouseRepository as Warehouse;
use App\Repositories\Eloquent\Exm\QuestionRepository as Question;
use App\Repositories\Eloquent\Edu\TuserRepository as Tuser;

class MyController extends Controller
{
    protected $tuser, $warehouse, $question;

    public function __construct(Weixin $wx, Tuser $tuser,Warehouse $warehouse, Question $question)
    {
        parent::__construct();

        $this->wx = $wx;

        $this->tuser = $tuser;

        $this->warehouse = $warehouse;

        $this->question = $question;

    }

    public function myActivity()
    {
        $user = Auth::guard()->user();

        $signs = $user->tracks;

        return view($this->view, compact('user', 'signs'));
    }


    public function myBorrow()
    {
        $user = Auth::guard()->user();

        $borrows = $user->borrows;

        return view($this->view, compact('user', 'borrows'));
    }


    public function myPoint()
    {
        $user = Auth::guard()->user();

        $points = $user->records;

        return view($this->view, compact('user', 'points'));
    }


    public function myDeploy()
    {
        $user = Auth::guard()->user();

        $terms = $user->terms()
            ->with(['users' =>function($query) use($user) {
                $query->where('uid', $user->id);
            }])->orderBy('id','desc')->get();

        //dd($terms);

        return view($this->view, compact('user', 'terms'));
    }


    public function myBook()
    {
        $user = Auth::guard()->user();

        $books = $user->books()->with('borrows')->withCount('borrows')->get();

        return view($this->view, compact('user', 'books'));
    }



    /**
     * 五一答题活动签到BEGIN
     * @return [type] [description]
     */
    public function myQuestion()
    {
        $can = false;

        $user = Auth::guard()->user();

        $warehouse = $this->warehouse->activity();

        if( !!$warehouse )
            $can = $this->isAnswer($warehouse, $user);

        return view($this->view, compact('user', 'warehouse','can'));
    }


    /**
     * 个人附件，附带班级备注信息
     * @param  Request $request [description]
     * @param  [type]  $id      [description]
     * @return [type]           [description]
     */
    public function myAttachment(Request $request, $id)
    {
        $user = Auth::guard()->user();

        $tuser = $this->tuser->show($id);
        return view($this->view, compact('user','tuser'));
    }


    public function postAttachmentAdd(Request $request)
    {
        return $this->tuser->attachmentAdd($request);
    }


    public function postAttachmentRemove(Request $request)
    {
        return $this->tuser->attachmentRemove($request);
    }


    public function postQuestion(Request $request)
    {
        $user = Auth::guard()->user();

        return $this->warehouse->setAnswer($user, $request);
    }


    public function isAnswer($warehouse, $user)
    {
        $uid = $warehouse->users->where('uid',$user->id)->first();

        if(!!$uid)
            return false;

        return true;
    }


    /**
     * 报名取消 added at 2021-713
     * @return [type] [description]
     */
    public function myCancel(Request $request)
    {
        $user = Auth::guard()->user();

        $tuser = $this->tuser->find($request->tuser);

        if($user->id !== $tuser->uid)
            return $this->responseFail(500, '数据不一致!');

        return $this->tuser->destroy($tuser->id);
    }

    /**
     * 资料修改 added at 2021-713
     * @return [type] [description]
     */
    public function myInfo()
    {
        $user = Auth::guard()->user();

        return view($this->view, compact('user'));
    }

    public function postInfo(Request $request)
    {
        return $this->wx->renew($request);
    }

}